package mhcs.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * 
 * @author Brenden Heath
 * A class for selecting test feeds of module landing information.
 */
public class GPSController extends HorizontalPanel {

	private int listNumber;
	final ListBox choices;
	final Button confirm;
	
	/**
	 * Constructor for this class.  Instanciates class members and then calls paint().
	 */
	public GPSController() {
		choices = new ListBox();
		confirm = new Button("Select");
		listNumber = 0;
		
		confirm.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent e) {
				listNumber = choices.getSelectedIndex() + 1;
				listChanged();
			}
		});
		
		choices.addItem("List 1");
		choices.addItem("List 2");
		choices.addItem("List 3");
		choices.addItem("List 4");
		choices.addItem("List 5");
		choices.addItem("List 6");
		choices.addItem("List 7");
		choices.addItem("List 8");
		choices.addItem("List 9");
		choices.addItem("List 10");
		choices.setVisibleItemCount(1);
				
		paint();
	}
	
	
	/**
	 * This method is called when the list item in the listbox is changed and the submit button 
	 * is called.  It updates the internally tracked list box number and builds a http string
	 * to get a JSON string loaded from the internet.
	 */
	private void listChanged() {
		if (listNumber == 0) {
			paint();
			return;
		}
		//remove the old modules possibly populating dataset
		MHCS.dataSet.clear();
		String proxy = "http://www.d.umn.edu/~heath088/Proxy.php?url=";
		String url = proxy + "http://www.d.umn.edu/~abrooks/SomeTests.php?q=" + listNumber;
		
		url = URL.encode(url);
		
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
		
		try {
			Request request = builder.sendRequest(null, new RequestCallback() {
				
				public void onError(Request request, Throwable exception) {
					Window.alert("onError: Couldn't retrieve JSON");
				}
				
				public void onResponseReceived(Request request, Response response) {
					if (200 == response.getStatusCode()) {
						String rt = response.getText();
						update(rt);
					} else {
						Window.alert("Couldn't retrieve JSON (" + response.getStatusText() + ")");
					}
						
				}
			});
		} catch (RequestException e) {
			Window.alert("RequestException: Couldn't retrieve JSON");
		}
	}
	
	/**
	 * This is method to get JSON string and parse it up and load it into our module
	 * dataset.
	 * @param rt is a JSON string which is to be parsed to get serial number, module status
	 * orientation, x and y coordinates.
	 */
	private void update (String rt) {	
		paint();

		String sAll = rt;

		JSONArray jA = (JSONArray)JSONParser.parseLenient(sAll);
		JSONNumber jN;
		JSONString jS;
		int srl;
		String stts;
		int rttn;
		int ex;
		int why;

		for (int i = 0; i < jA.size(); i++) { 
			JSONObject jO = (JSONObject)jA.get(i); 
			jN = (JSONNumber) jO.get("code"); 
			srl = (int)jN.doubleValue(); 

			jS = (JSONString) jO.get("status"); 
			stts = jS.stringValue(); 

			jN = (JSONNumber) jO.get("turns"); 
			rttn = (int)jN.doubleValue(); 

			jN = (JSONNumber) jO.get("X"); 
			ex = (int)jN.doubleValue(); 

			jN = (JSONNumber) jO.get("Y"); 
			why = (int)jN.doubleValue(); 

			try { 	
				MHCS.dataSet.add(srl, stts, rttn, ex, why);
			} catch (Exception e) {
				Window.alert(e.getMessage());
			}
		}
	} 

	
	/**
	 * This method redisplays the widgets within the GPS module.  First empty anything
	 * that was inside the panel and then repaint. 
	 */
	private void paint() {
		clear();
		HorizontalPanel hp = new HorizontalPanel();
		
		hp.add(choices);
		hp.add(confirm);
		add(hp);
	}

}
